home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / WWW / http / www.cu-amiga.co.uk / features / c-tutorial / Part-6.lzx / Part-6 / asl3 / loadsave.c < prev    next >
C/C++ Source or Header  |  1984-05-08  |  4KB  |  142 lines

  1. #include "loadsave.h"
  2. #include "bitmap.h"
  3. #include "drawwin.h"
  4. #include "gui.h"
  5.  
  6. #include "iff.h"
  7.  
  8. #include<libraries/asl.h>
  9.  
  10. #include<string.h>
  11. #include<stdio.h>
  12.  
  13. #include<clib/asl_protos.h>
  14. #include<clib/dos_protos.h>
  15. #include<clib/graphics_protos.h>
  16.  
  17. /* The size of our filename string */
  18. #define MAXFILENAME        (300)
  19.  
  20. /* Global handles for our requesters */
  21. static struct FileRequester* loadreq = NULL;
  22. static struct FileRequester* savereq = NULL;
  23.  
  24. /* Open an ASL load file requester */
  25. int load()
  26. {
  27.     /* Allocate the requester if we haven't already */
  28.     if(loadreq == NULL)
  29.         loadreq = (struct FileRequester*)
  30.             AllocAslRequestTags(ASL_FileRequest,
  31.                                                     ASLFR_TitleText,            "Load File",
  32.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS,
  33.                                                     ASLFR_InitialPattern,    "#?.iff",
  34.                                                     TAG_DONE);
  35.     if(loadreq)
  36.     {
  37.         struct Window* win = getDrawWin();
  38.         if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
  39.         {
  40.             char filename[MAXFILENAME];
  41.             /* Create complete filename from ASL's dir and file */
  42.             strcpy(filename, loadreq->rf_Dir);
  43.             if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
  44.             {
  45.                 IFFL_HANDLE handle;
  46.                 /* Try to open the IFF file */
  47.                 if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
  48.                 {
  49.                     UWORD colortable[256];
  50.                     /* Get colour information */
  51.                     LONG count = IFFL_GetColorTab(handle, colortable);
  52.                     /* Get display information */
  53.                     ULONG displayid = IFFL_GetViewModes(handle);
  54.                     /* Get picture information */
  55.                     struct IFFL_BMHD* bmhd = IFFL_GetBMHD(handle);
  56.                     /* Try to adjust the screen to fit */
  57.                     if(bmhd)
  58.                     {
  59.                         closeGUI();
  60.                         /* If we succeed then update our local win, else fail */
  61.                         if(openGUI(bmhd->nPlanes, bmhd->w, bmhd->h, displayid))
  62.                             win = getDrawWin();
  63.                         else
  64.                             return FALSE;
  65.                     }
  66.                     /* Change screen colours */
  67.                     LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
  68.                     /* If we can load the picture, update window's display */
  69.                     if(IFFL_DecodePic(handle, getBitmap()))
  70.                         CopySBitMap(win->WLayer);
  71.                     else
  72.                         printf("Error: could not decode IFF picture\n");
  73.                     IFFL_CloseIFF(handle);
  74.                 }
  75.                 else
  76.                     printf("Error: could not open IFF file\n");
  77.             }
  78.             else
  79.                 printf("Error: could not make filename\n");
  80.         }
  81.         /* else: requester was cancelled */
  82.     }
  83.     else
  84.         printf("Error: could not allocate ASL (load) file request\n");
  85.     /* If we get this far then there was no fatal error */
  86.     return TRUE;
  87. }
  88.  
  89. /* Open an ASL save file requester */
  90. void save()
  91. {
  92.     /* Another way of saying "allocate if we haven't already" */
  93.     if(savereq ||
  94.         (savereq = (struct FileRequester*)
  95.             AllocAslRequestTags(ASL_FileRequest,
  96.                                                     ASLFR_TitleText,            "Save File",
  97.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS | FRF_DOSAVEMODE,
  98.                                                     ASLFR_InitialPattern,    "#?.iff",
  99.                                                     ASLFR_InitialFile,        "picture.iff",
  100.                                                     TAG_DONE)))
  101.     {
  102.         struct Window* win = getDrawWin();
  103.         if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
  104.         {
  105.             char filename[MAXFILENAME];
  106.             /* Create complete filename from ASL's dir and file */
  107.             strcpy(filename, savereq->rf_Dir);
  108.             if(AddPart(filename, savereq->rf_File, MAXFILENAME))
  109.             {
  110.                 /* Make sure our bitmap is the same as the display */
  111.                 SyncSBitMap(win->WLayer);
  112.                 /* Try saving our bitmap, using the screen's colours */
  113.                 if(IFFL_SaveBitMap(filename, getBitmap(),
  114.                                                     win->WScreen->ViewPort.ColorMap->ColorTable,
  115.                                                     IFFL_COMPR_BYTERUN1) == 0)
  116.                     printf("Error: could not write IFF picture\n");
  117.             }
  118.             else
  119.                 printf("Error: could not make filename\n");
  120.         }
  121.         /* else: requester was cancelled */
  122.     }
  123.     else
  124.         printf("Error: could not allocate ASL (save) file request\n");
  125. }
  126.  
  127. /* Free any requesters that may have been allocated */
  128. void freeReqs()
  129. {
  130.     if(loadreq)
  131.     {
  132.         FreeAslRequest(loadreq);
  133.         loadreq = NULL;
  134.     }
  135.     if(savereq)
  136.     {
  137.         FreeAslRequest(savereq);
  138.         savereq = NULL;
  139.     }
  140. }
  141.  
  142.